﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using ViCode_LeVi.Reports;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabBalanceAndOthers_LIQUIDITYPLAN.xaml
    /// </summary>
    public partial class VMTabBalanceAndOthers_LIQUIDITYPLAN : UserControl
    {
        public VMTabBalanceAndOthers_LIQUIDITYPLAN()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VMTabBalanceAndOthers_Balance_Loaded);
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(VMTabBalanceAndOthers_LIQUIDITYPLAN_DataContextChanged);
        }

        void VMTabBalanceAndOthers_LIQUIDITYPLAN_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            RefreshView();
        }
        Step_BalanceAndOthers_LIQUIDITYPLAN _LIQUIDITYPLAN = null;
        void VMTabBalanceAndOthers_Balance_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshView();
        }
        private void RefreshView()
        {
            _LIQUIDITYPLAN = this.DataContext as Step_BalanceAndOthers_LIQUIDITYPLAN;
            if (_LIQUIDITYPLAN != null)
            {
                _LIQUIDITYPLAN.RefreshData();
                GenUI(_LIQUIDITYPLAN);
                SetupButton_AddNew();
            }
        }
        #region Gen
        private void GenUI(Step_BalanceAndOthers_LIQUIDITYPLAN balance)
        {
            if (balance == null)
                return;
            if (balance.Items_RECEIPTINCOME == null || balance.Items_RECEIPTINCOME.Length == 0)
            {
                balance.AddNew(balance.GROUP_RECEIPTINCOME);
            }
            GenUI(Panel_RECEIPTINCOME, balance.Items_RECEIPTINCOME);

            if (balance.Items_PAYMENTS == null || balance.Items_PAYMENTS.Length == 0)
            {
                balance.AddNew(balance.GROUP_PAYMENTS);
            }
            GenUI(Panel_PAYMENTS, balance.Items_PAYMENTS);

            if (balance.Items_ADDITIONALINCOME == null || balance.Items_ADDITIONALINCOME.Length == 0)
            {
                balance.AddNew(balance.GROUP_ADDITIONALINCOME);
            }
            GenUI(Panel_ADDITIONALINCOME, balance.Items_ADDITIONALINCOME);

            if (balance.Items_ADDITIONALPAYMENT == null || balance.Items_ADDITIONALPAYMENT.Length == 0)
            {
                balance.AddNew(balance.GROUP_ADDITIONALPAYMENT);
            }
            GenUI(Panel_ADDITIONALPAYMENT, balance.Items_ADDITIONALPAYMENT);

            if (balance.Items_STATUSBANKACCOUNT == null || balance.Items_STATUSBANKACCOUNT.Length == 0)
            {
                balance.AddNew(balance.GROUP_STATUSBANKACCOUNT, balance.GROUP_STATUSBANKACCOUNT);
            }
            GenUI(Panel_STATUSBANKACCOUNT, balance.Items_STATUSBANKACCOUNT);
            foreach (var ui in Panel_STATUSBANKACCOUNT.Children)
            {
                VMTabBalanceAndOthers_LIQUIDITYPLAN_Item item = ui as VMTabBalanceAndOthers_LIQUIDITYPLAN_Item;
                if (item != null)
                {
                    item.VMTextBox_Number_Year2.IsReadOnly = true;
                    item.VMTextBox_Number_Year3.IsReadOnly = true;
                    item.VMTextBox_Number_Year4.IsReadOnly = true;
                    item.VMTextBox_Number_Year5.IsReadOnly = true;
                }
            }

        }
        private void GenUI(Panel panel, Step_BalanceAndOthers_LIQUIDITYPLAN_Item[] items)
        {
            if (items == null || items.Length == 0)
            {
                panel.Children.Clear();
            }
            else
            {
                ////Collapse all
                //foreach(FrameworkElement child in panel.Children)
                //{
                //    child.Visibility = Visibility.Collapsed;
                //}
                //add
                var children = panel.Children.Cast<FrameworkElement>().ToArray();
                List<FrameworkElement> listInUI = new List<FrameworkElement>();
                foreach (var item in items)
                {
                    var child = children.FirstOrDefault(d => d.DataContext == item);
                    if (child == null)
                    {
                        child = new VMTabBalanceAndOthers_LIQUIDITYPLAN_Item()
                        {
                            DataContext = item
                              ,
                            Action_Remove = (removeItem) =>
                            {
                                if (_LIQUIDITYPLAN != null)
                                {
                                    if (MessageBox.Show("Do you want to remove this item?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                    {
                                        _LIQUIDITYPLAN.Remove(removeItem);
                                        if (removeItem.Parent != null)
                                            removeItem.Parent.SendPropertyChanged_Total();
                                        GenUI(_LIQUIDITYPLAN);
                                    }
                                }
                            }
                        };
                        panel.Children.Add(child);
                    }
                    listInUI.Add(child);
                }
                //remove old
                List<FrameworkElement> listWillRemove = new List<FrameworkElement>();
                foreach (FrameworkElement child in panel.Children)
                {
                    if (!listInUI.Contains(child))
                        listWillRemove.Add(child);
                }
                foreach (var removeItem in listWillRemove)
                {
                    panel.Children.Remove(removeItem);
                }
            }
        }
        #endregion

        #region Button add new
        private void SetupButton_AddNew()
        {
            Button_AddNew_RECEIPTINCOME.Click -= new RoutedEventHandler(Button_AddNew_RECEIPTINCOME_Click);
            Button_AddNew_RECEIPTINCOME.Click += new RoutedEventHandler(Button_AddNew_RECEIPTINCOME_Click);

            Button_AddNew_PAYMENTS.Click -= new RoutedEventHandler(Button_AddNew_PAYMENTS_Click);
            Button_AddNew_PAYMENTS.Click += new RoutedEventHandler(Button_AddNew_PAYMENTS_Click);

            Button_AddNew_ADDITIONALINCOME.Click -= new RoutedEventHandler(Button_AddNew_ADDITIONALINCOME_Click);
            Button_AddNew_ADDITIONALINCOME.Click += new RoutedEventHandler(Button_AddNew_ADDITIONALINCOME_Click);

            Button_AddNew_ADDITIONALPAYMENT.Click -= new RoutedEventHandler(Button_AddNew_ADDITIONALPAYMENT_Click);
            Button_AddNew_ADDITIONALPAYMENT.Click += new RoutedEventHandler(Button_AddNew_ADDITIONALPAYMENT_Click);

            //Button_AddNew_STATUSBANKACCOUNT.Click -= new RoutedEventHandler(Button_AddNew_STATUSBANKACCOUNT_Click);
            //Button_AddNew_STATUSBANKACCOUNT.Click += new RoutedEventHandler(Button_AddNew_STATUSBANKACCOUNT_Click);
        }

        void Button_AddNew_RECEIPTINCOME_Click(object sender, RoutedEventArgs e)
        {
            if (_LIQUIDITYPLAN != null)
                Button_AddNew(_LIQUIDITYPLAN.GROUP_RECEIPTINCOME);
        }
        void Button_AddNew_PAYMENTS_Click(object sender, RoutedEventArgs e)
        {
            if (_LIQUIDITYPLAN != null)
                Button_AddNew(_LIQUIDITYPLAN.GROUP_PAYMENTS);
        }
        void Button_AddNew_ADDITIONALINCOME_Click(object sender, RoutedEventArgs e)
        {
            if (_LIQUIDITYPLAN != null)
                Button_AddNew(_LIQUIDITYPLAN.GROUP_ADDITIONALINCOME);
        }
        void Button_AddNew_ADDITIONALPAYMENT_Click(object sender, RoutedEventArgs e)
        {
            if (_LIQUIDITYPLAN != null)
                Button_AddNew(_LIQUIDITYPLAN.GROUP_ADDITIONALPAYMENT);
        }
        void Button_AddNew_STATUSBANKACCOUNT_Click(object sender, RoutedEventArgs e)
        {
            if (_LIQUIDITYPLAN != null)
                Button_AddNew(_LIQUIDITYPLAN.GROUP_STATUSBANKACCOUNT, true);
        }
        void Button_AddNew(string grpName)
        {
            Button_AddNew(grpName, false);
        }
        void Button_AddNew(string grpName, bool just1Item)
        {
            if (_LIQUIDITYPLAN != null)
            {
                _LIQUIDITYPLAN.AddNew(grpName);
                GenUI(_LIQUIDITYPLAN);
            }
        }
        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Report_LiquidityPlan rep = new Report_LiquidityPlan();
            var datasource = new List<Step_BalanceAndOthers_LIQUIDITYPLAN>();
            datasource.Add(_LIQUIDITYPLAN);
            frmViewReport frm = new frmViewReport(rep, datasource);
            frm.Show();
        }
    }
}
